package com.example.controller;

import com.example.utils.*;
import com.example.utils.image.DrawArmsUtils;
import com.example.utils.image.RedisUtil;
import com.example.utils.json.Equipment;
import com.example.utils.json.EquipmentUtil;
import org.meowy.cqp.jcq.entity.CoolQ;
import org.meowy.cqp.jcq.message.CQCode;

import java.util.*;

import static java.util.Comparator.comparing;

public class EquipmentController {

    public String queryEquipment(String msg,Long fromGroup,Long fromQQ, CoolQ CQ){
        StringBuilder stringBuilder = new StringBuilder();
        String [] arr = msg.split("\\s+");
        CQCode CC = CQCode.getInstance();
        if(arr.length >1&&!StringUtils.isEmpty(arr[1])){
            // 装备名称
            String name = arr[1];
            String tempName = name;
            List<Equipment> equipmentList = EquipmentUtil.getEquipmentList();
            List<Equipment> reBackList = new ArrayList<Equipment>();


            // 如果装备带了系列 那么根据系列搜索
            if(name.contains("系列")||name.equals("系列")){
                name = name.replace("系列","");
                // 查询数据库是否有这个输入的系列 否则返回系列的字符串
                List<String> list = RedisUtil.getList(Config.REDIS_FILTER_SERIES);
                String seriesText = "";
                stringBuilder.append("当前游戏所有系列如下:\n");
                for (String s : list) {
                    if(StringUtils.isNotEmpty(name)&&s.contains(name)){
                        seriesText = s;
                        break;
                    }
                    stringBuilder.append("【").append(s).append("】\n");
                }
                // 如果name去除系列后为空 那么直接返回系列的字符串
                if(StringUtils.isEmpty(name)||StringUtils.isEmpty(seriesText)){
                    return stringBuilder.toString();
                }else {
                    tempName = seriesText;
                    //根据系列去查询装备
                    for (Equipment equipment : equipmentList) {
                        if(StringUtils.isNotEmpty(equipment.getSeriesText())&&equipment.getSeriesText().toUpperCase().contains(seriesText.toUpperCase())
                        ){
                            reBackList.add(equipment);
                        }
                    }
                    if(reBackList.size() <=0){
                        return stringBuilder.toString();
                    }
                    // 清空系列列表
                    stringBuilder.setLength(0);
                }
            // 其它一律当作根据名称查询
            }else if(name.contains("精英技能")||name.equals("精英技能")){
                name = name.replace("精英技能","");
            }else{
                // 默认根据名称搜索
                for (Equipment equipment : equipmentList) {
                    if(equipment.getTitle().toUpperCase().contains(name.toUpperCase())
                    ){
                        reBackList.add(equipment);
                    }
                }

                // 名称转换  主要是将常见名称 比如 泡泡枪 转为 尤格.索托斯
                List<String> equipmentAlias = RedisUtil.getList("coolq:equipmentAlias:"+name);
                if(equipmentAlias != null && equipmentAlias.size() >0){
                    for (String alias : equipmentAlias) {
                        for (Equipment equipment : equipmentList) {
                            if(equipment.getTitle().toUpperCase().contains(alias.toUpperCase())
                            ){
                                reBackList.add(equipment);
                            }
                        }
                    }
                }
                Map<Integer, Equipment> relationEquipment = null;
                // 获取该装备相关的装备
                for (Equipment equipment : reBackList) {
                    if(relationEquipment != null&&relationEquipment.containsKey(equipment.getUid())){
                        continue;
                    }
                    relationEquipment = EquipmentUtil.getRelationEquipment(equipment, relationEquipment);
                }
                // 将关联装备添加到查询的列表中
                if(relationEquipment != null){
                    Set<Integer> integers = relationEquipment.keySet();
                    for (Integer integer : integers) {
                        Equipment equipment = relationEquipment.get(integer);
                        reBackList.add(equipment);
                    }
                }
            }

            // 去除重复
            reBackList = EquipmentUtil.removeDistinctByIdAndTitle(reBackList);
            // 排序
            reBackList.sort(comparing(Equipment::getId));


            // 处理返回信息
            if(reBackList.size() > 6){
                stringBuilder.append("查询到 ").append(tempName).append(" 相关的装备如下,");
                stringBuilder.append("请使用以下关键字进行精确查询:").append("\n");
                Set<String> stringSet = new HashSet<String>();
                for (Equipment equipment : reBackList) {
                    stringSet.add(equipment.getTitle());
                }
                // 去除重复的名称
                for (String s : stringSet) {
                    stringBuilder.append("【").append(s).append("】\n");
                }
                stringBuilder.append("\n");
            }else if(reBackList.size()>0){
                for (Equipment equipment : reBackList) {
                    String image = DrawArmsUtils.getImage(equipment);
                    if(!StringUtils.isEmpty(image)){
                        stringBuilder.append(CC.image(image));
                    }
                }
            }else {
                stringBuilder.append("~~~没有找到您需要的装备~~~").append("\n");
                // 获取一张随机图片
                String string = RedisUtil.getString("coolq:role:delishaImagePath");
                if(StringUtils.isNotEmpty(string)){
                    List<String> fileNames = FileUtils.getFileNames(string);
                    if(fileNames.size() >0){
                        stringBuilder.append(CC.image("/role/delisha/"+ fileNames.get(MathUtils.randInt(fileNames.size(),0))));
                    }
                }
            }
        }else {
            stringBuilder.append("如果您想要查询装备请按照这个格式  查询 装备名称 | 查询 系列名称+\"系列\"结尾");
        }
        return stringBuilder.toString();
    }
}
